import { Component, OnInit } from '@angular/core';
import {FormGroup, FormBuilder, Validators} from '@angular/forms';
import {AuthUserService} from '../auth-user.service';
import {User} from '../model/user-model';
import {Router} from '@angular/router';

@Component({
  selector: 'app-auth-login',
  templateUrl: './auth-login.component.html',
  styleUrls: ['./auth-login.component.css']
})
export class AuthLoginComponent implements OnInit {

    private user: User = new User();
    public formGroup: FormGroup;
    public success = true;


    constructor(private authUserService: AuthUserService,
                private formBuilder: FormBuilder,
                private router: Router) {
    }

    ngOnInit() {

        this.formGroup = this.formBuilder.group({
            'username': [
                this.user.username, []
            ],
            'password': [
                this.user.password, []
            ]
        });
    }


    doLogin() {
        console.log(this.formGroup.value.password);
        this.authUserService.getUser(this.formGroup.value.username).subscribe(data => {
            this.user = data;
        });
        if (this.user && this.user.password === this.formGroup.value.password) {
            this.authUserService.setCurrentUser(this.user);
            this.router.navigateByUrl('/todo');
        } else {
            this.success = false;
        }
    }
}
